SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE  PROCEDURE dbo.paObtenerItemsDeMenu

@ItemPadre INT = NULL

AS
SET NOCOUNT ON
SELECT IdItemDeMenu, Nombre, Accion, ItemPadre, TieneSubitems, Icono
FROM tb_ItemsDeMenu ITM
INNER JOIN
	(
	SELECT PER.IdPermiso
	FROM tb_Permisos PER
	INNER JOIN tb_relUsuarioPermisos RUP ON PER.IdPermiso = RUP.IdPermiso
	INNER JOIN tb_Usuarios USU ON USU.IdUsuario = RUP.IdUsuario
	WHERE USU.NTUser = SYSTEM_USER

	UNION

	SELECT PER.IdPermiso
	FROM tb_Permisos PER
	INNER JOIN tb_relPermisosRoles RPR ON RPR.IdPermiso = PER.IdPermiso
	INNER JOIN tb_Roles ROL ON ROL.IdRol = RPR.IdRol
	INNER JOIN tb_relUsuarioRol RUR ON RUR.IdRol = ROL.IdRol
	INNER JOIN tb_Usuarios USU ON USU.IdUsuario = RUR.IdUsuario
	WHERE USU.NTUser = SYSTEM_USER
	) PER ON PER.IdPermiso = ITM.Permiso
WHERE (ITM.ItemPadre = @ItemPadre AND ITM.ItemPadre IS NOT NULL AND @ItemPadre IS NOT NULL) 
	OR (ITM.ItemPadre IS NULL AND @ItemPadre IS NULL)
ORDER BY Orden
GO
GRANT EXECUTE ON  [dbo].[paObtenerItemsDeMenu] TO [CentroDeDesignaciones]
GRANT EXECUTE ON  [dbo].[paObtenerItemsDeMenu] TO [public]
GRANT EXECUTE ON  [dbo].[paObtenerItemsDeMenu] TO [SoloVer]
GO
